package com.rey.demo.api;

import com.rey.demo.link.ZookeeperDemo;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * @ClassName: CreateApi
 * @Description: TODO
 * @Author reyJojo
 * @Date 2021/12/3 12:35
 * @Version 1.0
 */
public class CreateApi {
    public static void main(String[] args) {
        try {
            ZooKeeper zooKeeper = ZookeeperDemo.link4ZookeeperApi();
            if(zooKeeper != null) {
                // 1.1.创建zookeeper的znode
                System.out.println(zooKeeper.create("/test", "test node".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL));
                // 1.2.重复创建节点
//                System.out.println(zooKeeper.create("/test", "test node".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL));
                // 1.3.创建一个父节点不存在的子节点
//                System.out.println(zooKeeper.create("/father/son", "no father".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL));
                // 1.4.创建一个序列节点(持久化节点)
                System.out.println(zooKeeper.create("/seq_node", "sequential".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL));
                // 1.5.创建一个只读节点
                System.out.println(zooKeeper.create("/ro_node", "read only node".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT));
            }
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
